//登录标志,0:未登录状态;1:已登录状态
var loginFlag = 0;

//登录界面
applicationLoginPage = function() {
	MUI.Desktop.pageWrapper.empty();//删除所有元素
	$('loginUserInfo').empty();
	//重画登录界面
	new MUI.Column({
		id: 'leftColumn',
		placement: 'left',
		width:450,
		resizeLimit: [50, 500]
	});

	new MUI.Column({
		id: 'mainColumn',
		placement: 'main'
	});

	// 将宣传页显示到左边
	new MUI.Panel({
		id: 'login.welcome',
		header:false,
		contentURL: '/auth/login.welcome.action',
		//contentURL: '/mochaui/data/login.welcome.html',
		column: 'leftColumn'
	});
	

	// 登录输入页显示到右边
	new MUI.Panel({
		id: 'login.form',
		title: '登录',
		header:false,
		contentURL: '/auth/login.form.action',
		//contentURL: '/mochaui/data/login.form.html',
		column: 'mainColumn'
	});
	
	//MUI.myChain.callChain();
}

//应用主界面
applicationPage = function() {

	MUI.Desktop.pageWrapper.empty();//删除所有元素

	//左边列空间
	new MUI.Column({
		id: 'sideColumn1',
		placement: 'left',
		width: 200,
		resizeLimit: [100, 300]
	});
	//主列空间
	new MUI.Column({
		id: 'mainColumn',
		placement: 'main',
		resizeLimit: [100, 300]
	});

	// Add panels to first side column
	// 导航面板,组织面板
	new MUI.Panel({
		id: 'explorer-panel',
		title: '导航',		
		contentURL: '/auth/explorer.action',
		tabsURL: '/auth/explorer-tabs.action',
		//contentURL: '/mochaui/data/explorer.html',
		//tabsURL: '/mochaui/data/explorer-tabs.html',
		column: 'sideColumn1'
	});
	
	//处理用户信息
	findSession();

	//MUI.myChain.callChain();
};

//处理登录信息
findSession = function(){
	var url = '/data/sc.action';
	try {
	var request = new Request.JSON({
		url: url,
		onComplete: function(json,text) {
			var userinfo = "";
			userinfo += json.user.realname +'，欢迎使用本系统。<input type="button" id="logout" class="button" value="退出" />';
			$('loginUserInfo').set('html',userinfo);
			if ($('logout')){
				$('logout').addEvent('click', function(e) {
					new Event(e).stop();
					applicationLogout();//退出
				});
			}
		}//onComplete
	}).get({"scid":"session"});//request
	} catch (ex) {
		MUI.notification('<font color=red>错误：</font>登录信息访问失败('+ex+')');
	}
}

//退出
applicationLogout = function(){
	var myRequest = new Request({
    		method: 'get', 
    		url: '/auth/logout.action',
    		onSuccess: function(responseText, responseXML){
    			loginFlag = 0;
    			applicationLoginPage();
    		},
    		onFailure:function(instance){
    			MUI.notification('<font color=red>错误：</font>访问失败('+instance+')');
    		}
		}).send();
}

//判断用户的登录情况进行页面初始化
initialize = function(){
	//取用户的登录状态
	var url = '/auth/login_status.action';
	
	try {
	var request = new Request.JSON({
		url: url,
		onComplete: function(json,text) {
			//alert(text);
			if(json.loginStatus){//如果已处在登录状态
				applicationPage();//应用主界面
			}else{
				applicationLoginPage();//登录界面
			}
		}
	}).get();
	} catch (ex) {
		loginFlag = 0;
	}

}

confirmWindow = function(title,content){
	var confirmWindow = new MUI.Window({
	id: 'confirmWindow',
	title: title,
	loadMethod: 'html',
	//contentURL: 'pages/lipsum.html',
	content: content,
	width: 340,
	height: 150,
	minimizable:false,
	maximizable:false
	});
	return confirmWindow;
}


// 页面DOM结构载入完毕后执行
window.addEvent('load', function(){ 
	//隐藏dock栏
	$('dockWrapper').style.display="none";
	$('dock').style.display="none";
	//隐藏titlebar栏
	$('desktopTitlebarWrapper').style.display="none";
	$('desktopTitlebar').style.display="none";
	
	
	MUI.myChain = new Chain();//Chain的用法参考mootools文档
	
	MUI.myChain.chain(
		function(){MUI.Desktop.initialize();},
		function(){MUI.Dock.initialize();},
		function(){initialize();}
	).callChain();	

});

//导入所需css,无需等待页面DOM载入完毕。
/*
var csses = [MUI.path.themes + MUI.options.theme + '/css/Core.css',MUI.path.themes + MUI.options.theme + '/css/Content.css',MUI.path.themes + MUI.options.theme + '/css/Layout.css',MUI.path.themes + MUI.options.theme + '/css/Dock.css',MUI.path.themes + MUI.options.theme + '/css/Window.css',MUI.path.themes + MUI.options.theme + '/css/Tabs.css'];
new MUI.Require({
	css: csses
});
*/
